home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / akcl1615.lha / doc / akcl.el next >
Lisp/Scheme  |  1992-03-30  |  11KB  |  345 lines

  1. ;; Author William F. Schelter.   
  2.  
  3. ;; You should copy find-doc.el, akcl.el, lisp-complete.el to the emacs/lisp directory.
  4.  
  5. ;; Some commands and macros for dealing with lisp
  6. ;; M-X run : run akcl or another lisp
  7. ;; m-c-x ; evaluate defun in the other window or in the last lisp which you were using.
  8. ;; m-c-x ; with a numeric arg : compile the current defun in the other window
  9. ;; m-c-d ; disassemble in other window.
  10. ;; M-x macroexpand-next : macro expand the next sexp in other window.
  11. ;; C-h d Find documentation on symbol where the cursor is.
  12. ;; C-h / Find documentation on all strings containing a given string.
  13. ;; M-p complete the current input by looking back through the buffer to see what was last typed
  14. ;;        using this prompt and this beginning.   Useful in shell, in lisp, in gdb,...
  15.  
  16.  
  17. (setq lisp-mode-hook  'remote-lisp)
  18.  
  19. (autoload 'visit-doc-file "find-doc" nil t)
  20.  
  21. (autoload 'find-doc "find-doc" "Display documentation about STRING" t)
  22.  
  23. (autoload 'lisp-complete "lisp-complete" nil t)
  24.  
  25. (global-set-key "p" 'lisp-complete)
  26. (global-set-key "d" 'find-doc)
  27.  
  28. (defun remote-lisp (&rest l)
  29.   (and (boundp 'lisp-mode-map)
  30.        lisp-mode-map
  31.   (define-key lisp-mode-map "\e\C-d" 'lisp-send-disassemble)
  32.   (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun-compile)
  33.   (make-local-variable 'lisp-package)
  34.   (setq lisp-package nil)
  35.   (and (boundp 'remote-lisp-hook) (funcall remote-lisp-hook))
  36.   ))
  37.  
  38.  
  39. (defvar search-back-for-lisp-package-p nil)
  40.  
  41. ;; look at the beginning of buffer to try to find an in package statement
  42. (defun get-buffer-package ()
  43.  
  44.   "Returns what it thinks is the lisp package for the current buffer.
  45. It caches this information in the local variable `lisp-package'.  It
  46. obtains the information from searching for the first in-package from
  47. the beginning of the file.  Since in common lisp, there is only
  48. supposed to be one such statement, it should be able to determine
  49. this.  By setting lisp-package to t, you may disable its search.  This
  50. will also disable the automatic inclusion of an in-package statement
  51. in the tmp-lisp-file, used for sending forms to the current
  52. lisp-process."
  53.  
  54.   (cond ((eq lisp-package t) nil)
  55.     (search-back-for-lisp-package-p
  56.      (save-excursion
  57.        (cond ((re-search-backward "^[ \t]*(in-package "  nil t)
  58.           (goto-char (match-end 0))
  59.           (read (current-buffer))))))
  60.     (lisp-package lisp-package)
  61.     (t
  62.      (setq
  63.       lisp-package
  64.       (let (found success)
  65.         (save-excursion
  66.           (goto-char (point-min))
  67.           (while (not found)
  68.         (if (and (setq success (search-forward "(in-package " 1000 t))
  69.              (not (save-excursion
  70.                 (beginning-of-line)
  71.                 (looking-at "[ \t]*;"))))
  72.             (setq found  (read (current-buffer))))
  73.         (if (>= (point) 980) (setq found t))
  74.         (or success (setq found t))
  75.         ))
  76.         found)))))
  77.  
  78.  
  79. (defun run (arg)
  80.   "Run an inferior Lisp process, input and output via buffer *lisp*."
  81.   (interactive "sEnter name of file to run: ")
  82.   (require 'shell)
  83.   (setq lisp-mode-hook 'remote-lisp)
  84.   (switch-to-buffer  (make-shell (concat arg "-lisp") arg nil "-i"))
  85.   (make-local-variable 'shell-prompt-pattern)
  86.     (setq shell-prompt-pattern "^[^#%)>]*[#%)>]+ *")
  87.     (cond ((or (string-match "maxima" arg) (string-match "affine" arg)
  88.        (save-excursion     (sleep-for 2)
  89.                    (re-search-backward "maxima"
  90.                            (max 1 (- (point) 300))
  91.                            t)))
  92.        (require 'maxima-mode)
  93.        (inferior-maxima-mode)
  94.        (goto-char (point-max))
  95.        )
  96.       (t  (inferior-lisp-mode))))
  97.  
  98. (defun lisp-send-disassemble (arg)
  99.   (interactive "P")
  100.    (if  arg 
  101.        ( lisp-send-defun-compile "disassemble-h")
  102.             ( lisp-send-defun-compile "disassemble"))
  103.      )
  104.  
  105. (defvar time-to-throw-away nil)
  106. (defvar telnet-new-line "")
  107.  
  108. (defun lisp-send-defun-compile (arg)
  109.  
  110.   "Send the current defun (or other form) to the lisp-process.  If there
  111. is a numeric arg, the form (compile function-name) is also sent.  The
  112. value of lisp-process will be the process of the other exposed window (if
  113. there is one) or else the global value of lisp-process.  If the
  114. ...received message is not received, probably either the reading of
  115. the form caused an error.   If the process does not have telnet in
  116. its name, then we write a tmp file and load it.
  117. If :sdebug is in *features*, then si::nload is used instead of
  118. ordinary load, in order to record line information for debugging.
  119.  
  120. The value of `lisp-package' if non nil, will be used in putting an
  121. in-package statement at the front of the tmp file to be loaded.
  122. `lisp-package' is determined automatically on a per file basis,
  123. by get-buffer-package.
  124. "
  125.  
  126.   (interactive "P")
  127.   (other-window 1)
  128.   (let* ((proc (or (get-buffer-process (current-buffer)) lisp-process))
  129.      def beg
  130.      (this-lisp-process proc)
  131.      (lisp-buffer (process-buffer this-lisp-process))
  132.      fun)
  133.     (other-window 1)
  134.     (save-excursion
  135.       (end-of-defun)
  136.       (let ((end (dot)) (buffer (current-buffer))
  137.         (proc (get-process this-lisp-process)))
  138.     (setq lisp-process proc)
  139.     (beginning-of-defun)
  140.     (save-excursion
  141.       (cond ((and arg (looking-at "(def")) (setq def t))
  142.         (t (setq arg nil)))
  143.       (cond (def (forward-char 2)(forward-sexp 1)
  144.              (setq fun (read buffer))
  145.              (setq fun (prin1-to-string fun))
  146.              (message (format
  147.                    "For the lisp-process %s: %s"
  148.                    (prin1-to-string this-lisp-process) fun)))))
  149.     (cond ((eql (char-after (1- end)) ?\n)
  150.            (setq end (1- end)) ))
  151.     (setq beg (dot))
  152.     (my-send-region this-lisp-process beg end)
  153.     ))
  154.     
  155.  
  156.     (send-string this-lisp-process
  157.          (concat ";;end of form" "\n" telnet-new-line))
  158.     (cond (arg
  159.        (if (numberp arg) (setq arg "compile"))
  160.        (send-string this-lisp-process (concat "(" arg "'" fun ")"
  161.                           telnet-new-line))))
  162.     (and time-to-throw-away
  163.      (string-match "telnet"(buffer-name (process-buffer proc)))
  164.      (dump-output proc time-to-throw-away))
  165.     (cond (nil  (get-buffer-window lisp-buffer)
  166.           (select-window (get-buffer-window lisp-buffer))
  167.           (goto-char (point-max)))
  168.       (t nil))))
  169.  
  170. (defvar telnet-new-line "")
  171. (defvar tmp-lisp-file (concat "/tmp/" (user-login-name) ".lsp"))
  172.  
  173. (defun my-send-region (proc beg end)
  174.   (cond ((or (string-match "telnet" (process-name proc)))
  175.      (send-region proc beg end))
  176.     (t
  177.      (let ((package (get-buffer-package)))
  178.        (save-excursion
  179.          (let ((temp-buffer-show-hook '(lambda (x) nil)))
  180.            (with-output-to-temp-buffer  "*tmp-akcl*"
  181.          (if package
  182.              (prin1 (list 'in-package  package)))
  183.          (princ ";!(:line ")
  184.          (prin1
  185.            (let ((na (buffer-file-name (current-buffer))))
  186.              (if na (expand-file-name na)
  187.                (buffer-name (current-buffer))))
  188.            )
  189.          (princ (- (count-lines (point-min) (+ beg 5)) 1))
  190.          (princ ")\n")
  191.          (set-buffer "*tmp-akcl*")
  192.          (write-region (point-min) (point-max) tmp-lisp-file nil nil))))
  193.        (write-region beg end tmp-lisp-file t nil)
  194.        (message "sending ..")
  195.        (send-string
  196.         proc
  197.         (concat "(lisp::let ((*load-verbose* nil)) (#+sdebug si::nload #-sdebug load \""
  198.             tmp-lisp-file
  199.             "\")#+akcl(setq si::*no-prompt* t)(values))\n  ")
  200.             )
  201.        (message (format "PACKAGE: %s ..done" (or package "none")))
  202.  
  203.        ))))
  204.  
  205. (defun dump-output (proc seconds)
  206.   "dump output for PROCESS for SECONDS or to \";;end of form\""
  207.  (let ((prev-filter (process-filter proc)) (already-waited 0))
  208.        (unwind-protect (progn (set-process-filter proc 'dump-filter)
  209.                   (while (< already-waited seconds)
  210.                   (sleep-for 1)(setq already-waited
  211.                          (1+ already-waited))))
  212.      (set-process-filter proc prev-filter))))
  213.  
  214.  
  215.  
  216. (defun dump-filter (proc string)
  217. ;  (setq she (cons string she))
  218.   (let ((ind (string-match ";;end of form" string)))
  219.     (cond (ind (setq string (substring
  220.                  string
  221.                  (+ ind (length
  222.                      ";;end of form"))))
  223.  
  224.            (message "... received.")
  225.            (setq already-waited 1000)
  226.            (set-process-filter proc prev-filter)
  227.            (cond (prev-filter (funcall prev-filter proc string))
  228.              (t string)))
  229.       (t ""))))
  230.  
  231.  
  232. ;;(process-filter (get-process "lisp"))
  233. (defun macroexpand-next ()
  234.   "macroexpand current form"
  235.   (interactive)
  236.   (save-excursion
  237.     (let ((beg (point)))
  238.       (forward-sexp )
  239.       (message "sending macro")
  240.  
  241.       (let* ((current-lisp-process
  242.           (or (get-buffer-process (current-buffer))
  243.                (prog2 (other-window 1)
  244.                   (get-buffer-process (current-buffer))
  245.                   (other-window 1)))))
  246.     (send-string current-lisp-process "(macroexpand '")
  247.     (send-region current-lisp-process  beg (point) )
  248.     (send-string current-lisp-process ")\n")))))
  249.  
  250. (defun delete-comment-char (arg) 
  251.   (while (and (> arg 0) (looking-at comment-start)) (delete-char 1) 
  252.      (setq arg (1- arg))))
  253.  
  254. (defun mark-long-comment ()
  255.   (interactive)
  256.   (let ((at (point)))
  257.     (beginning-of-line)
  258.     (while(and (not (eobp))
  259.            (or  (looking-at comment-start)
  260.             ;(looking-at "[     ]*\n")
  261.             ))
  262.       (forward-line 1))
  263.     (set-mark (point))
  264.     (goto-char at)
  265.     (while(and (not (bobp))
  266.            (or  (looking-at comment-start)
  267.             ;(looking-at "[     ]*\n")
  268.             ))
  269.       (forward-line -1))
  270.     (or (bobp )(forward-line 1))))
  271.     
  272.  
  273. (defun fill-long-comment ()
  274.   (interactive)
  275.   (mark-long-comment)
  276.   (let ((beg (min (dot) (mark)))
  277.     (end (max (dot) (mark))) (n 0)m)
  278.     (narrow-to-region beg end)
  279.     (goto-char (point-min))    
  280.     (while (looking-at ";")
  281.       (forward-char 1))
  282.     (setq n (- (point) beg))
  283.     (goto-char (point-min))    
  284.     (while (not (eobp))
  285.       (setq m n)
  286.       (while (> m  0)
  287.     (cond ((looking-at ";")
  288.            (delete-char 1)
  289.            (cond ((looking-at " ")(delete-char 1)(setq m 0)))
  290.            (setq m (- m 1)))
  291.           (t (setq m 0))))
  292.       (forward-line 1))
  293.     (fill-region (dot-min) (dot-max))
  294.     (goto-char (point-min))
  295.     (while (not (eobp))
  296.       (cond ((looking-at "\n")
  297.          nil)
  298.         (t(insert ";; ")))
  299.       (forward-line 1))
  300.    (goto-char (point-min))
  301.    (set-mark (point-max))
  302.    (widen)))
  303.  
  304. (defun comment-region (arg) 
  305.   "Comments the region, with a numeric arg deletes up to arg comment 
  306. characters from the beginning of each line in the region.  The region stays, 
  307. so a second comment-region adds another comment character" 
  308.  (interactive "P") 
  309.  (save-excursion 
  310.    (let ((beg (dot)) 
  311.      (ok t)(end (mark)))
  312.           (comment-region1 beg end arg))))
  313.  
  314. (defun comment-region1 (beg end arg)
  315.   (let ((ok t))
  316.     (cond((> beg end) 
  317.       (let ((oth end)) 
  318.         (setq end beg beg oth)))) 
  319.     (narrow-to-region beg end) 
  320.     (goto-char beg) 
  321.        (unwind-protect 
  322.        (while ok 
  323.          (cond (arg 
  324.             (delete-comment-char arg)) 
  325.            (t   (insert-string comment-start)))
  326.          (if (< end (dot)) (setq ok nil)
  327.            (if  (search-forward "\n" end t) nil (setq ok nil))) )
  328.      (widen))))
  329.  
  330. (defun trace-expression ()
  331.   (interactive)
  332.   (save-excursion
  333.      (forward-sexp )
  334.     (let ((end (point)))
  335.            (forward-sexp -1)
  336.       (other-window 1)
  337.       (let* ((proc (get-buffer-process (current-buffer)))
  338.          (current-lisp-process (or  proc lisp-process)))
  339.     (other-window 1)
  340.     (message "Tracing: %s" (buffer-substring (point) end))
  341.     (send-string current-lisp-process "(trace ")
  342.     (send-region current-lisp-process (point) end)
  343.     (send-string current-lisp-process ")\n")))))
  344.  
  345. (provide 'akcl)